//package com.coinworld.admin.aspect;
//
//import com.alibaba.fastjson.JSON;
//import com.coinworld.admin.annotation.Log;
//import com.coinworld.admin.utils.CommonUtils;
//import com.coinworld.constants.RedisIndex;
//import com.coinworld.dao.SystemLogDao;
//import com.coinworld.entity.Account;
//import com.coinworld.entity.SystemLog;
//import com.coinworld.object.HttpJsonResult;
//import com.coinworld.redis.RedisPool;
//import com.coinworld.response.ResponseCode;
//import com.coinworld.response.Result;
//import com.coinworld.utils.AppContextUtil;
//import com.coinworld.utils.IPUtils;
//import com.coinworld.utils.JSONUtils;
//import net.sf.json.JSONObject;
//import org.apache.commons.lang.ArrayUtils;
//import org.aspectj.lang.ProceedingJoinPoint;
//import org.aspectj.lang.annotation.Around;
//import org.aspectj.lang.annotation.Aspect;
//import org.aspectj.lang.annotation.Pointcut;
//import org.aspectj.lang.reflect.MethodSignature;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
//import org.springframework.util.CollectionUtils;
//import org.springframework.util.StringUtils;
//
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.lang.reflect.Method;
//import java.util.*;
//import java.util.stream.Collectors;
//import java.util.stream.Stream;
//
//
///**
// * @description:
// * @author:
// * @date: 2020/4/17 10:55
// */
//@Aspect
//@Component
//public class SysLogAspect {
//
//    /** logger */
//    private static final Logger LOGGER = LoggerFactory.getLogger(SysLogAspect.class);
//
//	@Autowired
//    private SystemLogDao sysLogDao;
//	@Autowired
//    private CommonUtils commonUtils;
//	@Autowired
//    private RedisPool redisPool;
//
//	@Pointcut("execution(public * com.honey.trip.admin.controller.*.*(..))")
//	public void logPointCut() {}
//
//	@Around("logPointCut()")
//	public Object around(ProceedingJoinPoint point) throws Throwable {
//		long beginTime = System.currentTimeMillis();
//		// 目标方法
//		Object result = point.proceed();
//		long time = System.currentTimeMillis() - beginTime;
//		// 保存日志
//        try {
//            saveLog(point, time,result);
//        } catch (Exception e) {
//            LOGGER.error("==================================> saveSysLog.around.exception: " + e.getMessage(), e);
//        }
//		return result;
//	}
//
//	private void saveLog(ProceedingJoinPoint joinPoint, long time,Object resultParam) {
//		MethodSignature signature = (MethodSignature) joinPoint.getSignature();
//        Method method = signature.getMethod();
//        SystemLog sysLog = new SystemLog();
//		// 请求的方法名
//		String className = joinPoint.getTarget().getClass().getName();
//		String methodName = signature.getName();
//
//		sysLog.setServiceName(className + "." + methodName + "()");
//        Log syslog = method.getAnnotation(Log.class);
//        if (syslog != null) {
//            // 注解上的描述
//            sysLog.setOperation(syslog.value());
//        }
//		// 请求的参数
//		Object[] args = joinPoint.getArgs();
//        Stream<?> stream = ArrayUtils.isEmpty(args) ? Stream.empty() : Arrays.asList(args).stream();
//        List<Object> logArgs = stream
//                .filter(arg -> (!(arg instanceof HttpServletRequest) && !(arg instanceof HttpServletResponse)))
//                .collect(Collectors.toList());
//        //过滤后序列化无异常
//        logArgs.removeAll(Collections.singleton(null)); //移除所有的null元素
//        if (!CollectionUtils.isEmpty(logArgs)){
//            String param = "";
//            try {
//                param = JSON.toJSONString(logArgs);
//            }catch (Exception e){
//                LOGGER.info("saveLog param unknown type  str:{}",logArgs);
//            }
//            sysLog.setParams(param);
//        }
//        HttpServletRequest request = AppContextUtil.getHttpServletRequest();
//        Account account = commonUtils.getAccount(request);
//        sysLog.setClientIp(IPUtils.getIpAddr(request));
//        JSONObject result = JSONUtils.readValue(JSONUtils.writeString(resultParam),JSONObject.class);
//        String resultCode = "";
//        Object resultMsg = "";
//        if (resultParam instanceof HttpJsonResult){
//            resultCode = result.getBoolean("success")+"";
//            resultMsg = result.get("msg");
//        }else if (resultParam instanceof Result){
//            resultCode = result.get("code")+"";
//            resultMsg = result.get("msg");
//        }
//        sysLog.setResultCode(resultCode);
//        if (!StringUtils.isEmpty(resultMsg)){
//            sysLog.setResultErrorMsg(resultMsg+"");
//        }
//        if ("login".equals(methodName)){
//            if (Integer.valueOf(resultCode)==ResponseCode.Common.SUCCESS.getCode()){
//                JSONObject resultObj = result.getJSONObject("data");
//                account = JSONUtils.readValue(redisPool.get(resultObj.getString("token"), RedisIndex.ONE),Account.class);
//            }
//        }
//        if (Objects.isNull(account)) {
//            sysLog.setAccountId(-1);
//            sysLog.setAccountName("获取用户信息为空");
//		} else {
//			sysLog.setAccountId(account.getId());
//            sysLog.setAccountName(account.getAccountName());
//        }
//		sysLog.setTotalMs((int) time);
//		sysLog.setCreateTime(new Date());
//		// 保存系统日志
//		sysLogDao.insertSelective(sysLog);
//	}
//
//
//}
